package cn.le.javaNet;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class JabberServer {

    public static final int PORT = 8080;

    public static void main(String[] args) throws IOException {
        Socket socket = null;
        try {
            ServerSocket serverSocket = new ServerSocket(PORT);
            //侦听并接受到此套接字的连接
            socket = serverSocket.accept();
            //获取此套接字的输入流
            BufferedReader in = new BufferedReader(
                        new InputStreamReader(socket.getInputStream()));

            //获取此套接字的输出流
            PrintWriter out = new PrintWriter(
                    new BufferedWriter(new OutputStreamWriter(
                            socket.getOutputStream())),true);
            System.out.println("111");
            while (true)
            {
                String str = in.readLine();
                if(str.equals("END"))
                {
                    break;
                }
                System.out.println("Echoing: "+str);
                out.println(str);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
        finally
        {
            System.out.println("closing");
            try {
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
    class JabberClient{

    public static void main(String[] args) throws IOException {

        Socket socket = new Socket("127.0.0.1",8080);

        BufferedReader in = new BufferedReader(
                new InputStreamReader(socket.getInputStream())
        );

        PrintWriter out = new PrintWriter(
                new BufferedWriter(
                        new OutputStreamWriter(
                                socket.getOutputStream())),true
        );

       /* for (int i = 0; i < 10; i++) {
            out.println("howdy "+i);
            String str = in.readLine();
            System.out.println(str);
        }*/

        //out.println("END");
    }
}